#!/bin/bash

set -e
set -x

# Usage: put the relevant guile .tar.gz file into the current
# directory and run the script.  The resulting two orig.tar.gz files
# will be placed into a new ./split-tmp directory.

src_name="guile"
src_ver="1.8.7"
src_dir="${src_name}-${src_ver}"
src_archive="${src_name}-${src_ver}.tar.gz"

deb_pkg_name="guile-1.8"
deb_src_rev="1"

non_main_dir="${src_dir}-non-dfsg"

function copy_to_non_main_dir()
{
  pushd "${src_dir}"
  cp -a --parents "$1" "../${non_main_dir}/"
  popd
}

# assumes someone has already copied file to non-main dir
function copy_to_main_dir()
{
  pushd "${non_main_dir}"
  cp -a --parents "$1" "../${src_dir}/"
  popd
}

if ! test root = "`whoami`"
then
  echo Use fakeroot.
  exit 1
fi

if test -e split-tmp
then
  echo "./split-tmp already exists - aborting"
  exit 1
fi

mkdir ./split-tmp
pushd split-tmp

umask 002

tar xzf "../${src_archive}"
mkdir "${non_main_dir}"

# remove trash
rm "${src_dir}"/srfi/srfi-1.x
rm "${src_dir}"/srfi/srfi-13.x
rm "${src_dir}"/srfi/srfi-14.x
rm "${src_dir}"/srfi/srfi-4.x
rm "${src_dir}"/srfi/srfi-60.x
rm "${src_dir}"/test-suite/standalone/test-asmobs-lib.x
rm "${src_dir}"/test-suite/standalone/test-fast-slot-ref
rm "${src_dir}"/test-suite/standalone/test-use-srfi

# So both sides will have a copy
copy_to_non_main_dir "COPYING.LESSER"
copy_to_non_main_dir "GUILE-VERSION"
copy_to_non_main_dir "LICENSE"
copy_to_non_main_dir "am"
copy_to_non_main_dir "doc/ChangeLog-2008"
copy_to_non_main_dir "doc/Makefile.am"
copy_to_non_main_dir "doc/NEWS"
copy_to_non_main_dir "doc/README"
copy_to_non_main_dir "doc/THANKS"
copy_to_non_main_dir "guile-config/guile.m4"

# verbatim dist only
copy_to_non_main_dir "doc/goops"
rm -r "${src_dir}/doc/goops"

# Files which are not DFSG compliant because they're licensed under
# the GFDL with invariant sections (front and back cover texts are
# also invariant), and files associated with those files.
copy_to_non_main_dir "doc/example-smob"
copy_to_non_main_dir "doc/ref"
rm -r "${src_dir}/doc/example-smob"
rm -r "${src_dir}/doc/ref"

GZIP=-9v tar czpSf \
  "${deb_pkg_name}_${src_ver}+${deb_src_rev}.orig.tar.gz" \
  "${src_dir}"

GZIP=-9v tar czpSf \
  "${deb_pkg_name}-non-dfsg_${src_ver}+${deb_src_rev}.orig.tar.gz" \
  "${non_main_dir}"

exit 0
